﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataAccessLogic;

namespace BusinessLogic
{
    [Serializable]
    public class Relationship : Table
    {
        #region Fields
        private RelationshipType _relationshipTypeCode;
        private RelationshipType _reciprocalRelationshipTypeCode;
        private Constituent _relationshipConstituent;
        private Constituent _reciprocalConstituent;
        private bool _isPrimary;
        #endregion

        #region Methods
        public bool CreateForConversion()
        {
            try
            {
                this._id = RelationshipDataAccess.CreateForConversion(this);

                if (this._id != Guid.Empty)
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }

            return false;
        }

        public bool Create()
        {
            try
            {
                this._id = RelationshipDataAccess.Create(this);
            
                if(this._id != Guid.Empty)
                {
                    return true;
                }
            }
            catch
            {
                return false;   
            }

            return false;
        }

        public void Delete()
        {
            RelationshipDataAccess.Delete(this._id);
        }

        public static void DeleteByConstituentID(Guid constituentID)
        {
            RelationshipDataAccess.DeleteByConstituentID(constituentID);
        }

        public static List<Relationship> GetAllByConstituentID(Guid constituentID)
        {
            return RelationshipDataAccess.GetAllByConstituentID(constituentID);
        }

        public static Relationship GetByID(Guid id)
        {
            return RelationshipDataAccess.GetByID(id);
        }

        public void Update()
        {
            RelationshipDataAccess.Update(this);
        }
        #endregion

        #region Properties
        public RelationshipType RelationshipTypeCode
        {
            get { return _relationshipTypeCode; }
            set { _relationshipTypeCode = value; }
        }

        public RelationshipType ReciprocalRelationshipTypeCode
        {
            get { return _reciprocalRelationshipTypeCode; }
            set { _reciprocalRelationshipTypeCode = value; }
        }

        public Constituent RelationshipConstituent
        {
            get { return _relationshipConstituent; }
            set { _relationshipConstituent = value; }
        }
        
        public Constituent ReciprocalConstituent
        {
            get { return _reciprocalConstituent; }
            set { _reciprocalConstituent = value; }
        }

        public Constituent Constituent
        {
            get 
            {
                if(null != _reciprocalConstituent && Guid.Empty != _reciprocalConstituent.ID)
                {
                    return _reciprocalConstituent;
                }

                return _relationshipConstituent;
            }
        }

        public RelationshipType Type
        {
            get 
            {
                if (null != _reciprocalConstituent && Guid.Empty != _reciprocalConstituent.ID)
                {
                    return _reciprocalRelationshipTypeCode;
                }

                return _relationshipTypeCode;
            }
        }

        public bool IsPrimary
        {
            get
            {
                return _isPrimary;
            }
            set
            {
                _isPrimary = value;
            }
        }
        #endregion
    }
}